<?php
// +----------------------------------------------------------------------
// | ThinkPHP
// +----------------------------------------------------------------------
// | Copyright (c) 2007 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

/* --------------------公共函数----------------------*/

	//通过玩家AutoId找到玩家编号user_id
	function user_id($id){
		$rs = M ('fck') -> where('id ='.$id) -> field('user_id') -> find();
		return $rs['user_id'];
	}
	
	function cp_name($cid){
		$rs = M ('cptype') -> where('id ='.$cid) -> field('tpname') -> find();
		if($rs){
			return $rs['tpname'];
		}else{
			return "无";
		}
	}



	 /*
    *获取推荐人
    *
    *$ID            会员ID
    *$myid          获奖会员ID
    *$inUserID      获奖会员编号
    *$money         奖金金额
    *$money_min     最小注册金额
    *
    */
    function member_list($ID=0,$max_num=0,$data=array(),$i=0){
    		if($max_num>=$i || $max_num==0){
    		$fck   = M('fck');
    		$where = array();
    		$where['id'] = $ID;
    		$field = 'id,re_id,u_level,re_level,is_fx,dl_level';
    		$frs   = $fck->where($where)->field($field)->find();
    		 if($frs){
    		 	//echo $frs['id']."<br>";
    		 	$data[$i] = $frs;
				$i++;
    			$data=member_list($frs['re_id'],$max_num,$data,$i);
    		 }
    			 unset($fck,$where,$field,$frs,$i); 
    		}
    		return $data;
    }
	
	function mysubstr($string, $sublen, $start = 0, $code = 'UTF-8'){
		//字符串截取函数 默认UTF-8
		if($code == 'UTF-8'){
			$pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
			preg_match_all($pa, $string, $t_string);
	
			if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."...";
			return join('', array_slice($t_string[0], $start, $sublen));
		}else{
			$start = $start*2;
			$sublen = $sublen*2;
			$strlen = strlen($string);
			$tmpstr = '';
	
			for($i=0; $i< $strlen; $i++)
			{
				if($i>=$start && $i< ($start+$sublen))
				{
					if(ord(substr($string, $i, 1))>129)
					{
						$tmpstr.= substr($string, $i, 2);
					}
					else
					{
						$tmpstr.= substr($string, $i, 1);
					}
				}
				if(ord(substr($string, $i, 1))>129) $i++;
			}
			if(strlen($tmpstr)< $strlen ) $tmpstr.= "...";
			return $tmpstr;
		}
	}
	
	function mytelsubstr($string, $sublen, $start = 0, $code = 'UTF-8'){
		//字符串截取函数 默认UTF-8
		if($code == 'UTF-8'){
			$pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
			preg_match_all($pa, $string, $t_string);
	
			if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."****";
			return join('', array_slice($t_string[0], $start, $sublen));
		}else{
			$start = $start*2;
			$sublen = $sublen*2;
			$strlen = strlen($string);
			$tmpstr = '';
	
			for($i=0; $i< $strlen; $i++)
			{
				if($i>=$start && $i< ($start+$sublen))
				{
					if(ord(substr($string, $i, 1))>129)
					{
						$tmpstr.= substr($string, $i, 2);
					}
					else
					{
						$tmpstr.= substr($string, $i, 1);
					}
				}
				if(ord(substr($string, $i, 1))>129) $i++;
			}
			if(strlen($tmpstr)< $strlen ) $tmpstr.= "****";
			return $tmpstr;
		}
	}

	//如果user_id等于800000就返回公司
	function conname($n){
		$rs = M ('fck') -> where('id =1') -> field('user_id') -> find();
		if($n == $rs['user_id']){
			return '公司';
		}else{
			return $n;
		}
	}

	function connames($n){
		$id = $_SESSION[C('USER_AUTH_KEY')];
		if($n == 0){
			return '会员';
		}
		if($n == 1){
			return '主任';
		}
		if($n == 2){
			return '经理';
		}
		if($n == 3){
			return '高级经理';
		}
		if($n == 4){
			return '总监';
		}
		if($n == 5){
			return '高级总监';
		}
		if($n == 6){
			return '一星董事';
		}
		if($n == 7){
			return '二星董事';
		}
		if($n >= 8){
			return '三星董事';
		}

	}
	function leve($n){
		$id = $_SESSION[C('USER_AUTH_KEY')];
		if($n == 0){
			return '会员';
		}
		if($n == 1){
			return '银卡';
		}
		if($n == 2){
			return '金卡';
		}
		if($n == 3){
			return '铂金卡';
		}
		if($n == 4){
			return '钻石卡';
		}
		if($n == 5){
			return '蓝钻卡';
		}


	}

	function pleve($n){
		$id = $_SESSION[C('USER_AUTH_KEY')];
		if($n == 0){
			return '一级';
		}
		if($n == 1){
			return '二级';
		}
		if($n == 2){
			return '三级';
		}
		if($n == 3){
			return '四级';
		}
		if($n == 4){
			return '五级';
		}
		if($n == 5){
			return '六级';
		}
		if($n == 6){
			return '七级';
		}
		if($n == 7){
			return '八级';
		}
		if($n == 8){
			return '九级';
		}
		if($n == 9){
			return '十级';
		}
		if($n == 10){
			return '十一级';
		}
		if($n == 11){
			return '十二级';
		}
		if($n == 12){
			return '十三级';
		}
		if($n == 13){
			return '十四级';
		}
		if($n == 14){
			return '十五级';
		}
		if($n == 15){
			return '十六级';
		}
		if($n == 16){
			return '十七级';
		}
		if($n == 17){
			return '十八级';
		}
		if($n == 18){
			return '十九级';
		}
		if($n == 19){
			return '二十级';
		}
		if($n == 20){
			return '二十级';
		}


	}

	function pwdHash($password, $type = 'md5') {
		return hash ( $type, $password );
	}
	//对密码进行加密
	function pwdHash_pass($password, $type = 'md5') {
		return hash ( $type, $password );
	}
	
	function noHTML($content){
		$content = strip_tags($content);
		$content = preg_replace("/<a[^>]*>/i",'', $content);
		$content = preg_replace("/<\/a>/i", '', $content);
		$content = preg_replace("/<div[^>]*>/i",'', $content);
		$content = preg_replace("/<\/div>/i",'', $content);
		$content = preg_replace("/<font[^>]*>/i",'', $content);
		$content = preg_replace("/<\/font>/i",'', $content);
		$content = preg_replace("/<p[^>]*>/i",'', $content);
		$content = preg_replace("/<\/p>/i",'', $content);
		$content = preg_replace("/<span[^>]*>/i",'', $content);
		$content = preg_replace("/<\/span>/i",'', $content);
		$content = preg_replace("/<\?xml[^>]*>/i",'', $content);
		$content = preg_replace("/<\/\?xml>/i",'', $content);
		$content = preg_replace("/<o:p[^>]*>/i",'', $content);
		$content = preg_replace("/<\/o:p>/i",'', $content);
		$content = preg_replace("/<u[^>]*>/i",'', $content);
		$content = preg_replace("/<\/u>/i",'', $content);
		$content = preg_replace("/<b[^>]*>/i",'', $content);
		$content = preg_replace("/<\/b>/i",'', $content);
		$content = preg_replace("/<meta[^>]*>/i",'', $content);
		$content = preg_replace("/<\/meta>/i",'', $content);
		$content = preg_replace("/<!--[^>]*-->/i",'', $content);
		$content = preg_replace("/<p[^>]*-->/i",'', $content);
		$content = preg_replace("/style=.+?['|\"]/i",'',$content);
		$content = preg_replace("/class=.+?['|\"]/i",'',$content);
		$content = preg_replace("/id=.+?['|\"]/i",'',$content);
		$content = preg_replace("/lang=.+?['|\"]/i",'',$content);
		$content = preg_replace("/width=.+?['|\"]/i",'',$content);
		$content = preg_replace("/height=.+?['|\"]/i",'',$content);
		$content = preg_replace("/border=.+?['|\"]/i",'',$content);
		$content = preg_replace("/face=.+?['|\"]/i",'',$content);
		$content = preg_replace("/face=.+?['|\"]/",'',$content);
		$content = preg_replace("/face=.+?['|\"]/",'',$content);
		$content = str_replace( " ","",$content);
		$content = str_replace( "&nbsp;","",$content);
		return $content;
	}
	
	/**
	* 查询信誉
	**/
	function cx_usrate($myid){
		$fck = M ('fck');
		$mrs = $fck->where('id='.$myid)->field('id,seller_rate')->find();
		$mrate = (int)$mrs['seller_rate'];
		$s_img = "";
		if($mrate>0){
			for($i=1;$i<=$mrate;$i++){
				$s_img .='<img src="__PUBLIC__/Images/star.gif" />';
			}
		}
		unset($fck,$mrs);
		return $s_img;
	}
	
	/**
	 * 给出兑换货币
	 * **/
	function Jx_cname($brmb){
		$fee = M ('fee');
		$fee_rs = $fee->field('str10,str11')->find();
		$prii = $fee_rs['str10'];
		$ormb = $brmb*$prii;
		$ormb = number_format($ormb,2);
		$in_r = "$".$ormb;
		unset($fee,$fee_rs);
		return $in_r;
	}
	function cx_cname($brmb){
		$fee = M ('fee');
		$fee_rs = $fee->field('str10,str11')->find();
		$prii = $fee_rs['str11'];
		$ormb = $brmb*$prii;
		$ormb = number_format($ormb,2);
		$out_r = "$".$ormb;
		unset($fee,$fee_rs);
		return $out_r;
	}
	
	function zh_filesize($fsize){
		$mbb = 1024;
		$gbb = 1024*1024;
		if($fsize>=$gbb){
			$out_s = $fsize/$gbb;
			$out_s = ((int)($out_s*100))/100;
			$last_o = $out_s." GB";
		}elseif($fsize>=$mbb){
			$out_s = $fsize/$mbb;
			$out_s = ((int)($out_s*100))/100;
			$last_o = $out_s." MB";
		}else{
			$out_s = number_format($fsize,2);
			$last_o = $out_s." KB";
		}
		return $last_o;
	}
function getfirstchar($s0){   //获取单个汉字拼音首字母。注意:此处不要纠结。汉字拼音是没有以U和V开头的
    $fchar = ord($s0{0});
    if($fchar >= ord("A") and $fchar <= ord("z") )return strtoupper($s0{0});
    $s1 = iconv("UTF-8","gb2312", $s0);
    $s2 = iconv("gb2312","UTF-8", $s1);
    if($s2 == $s0){$s = $s1;}else{$s = $s0;}
    $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
    if($asc >= -20319 and $asc <= -20284) return "A";
    if($asc >= -20283 and $asc <= -19776) return "B";
    if($asc >= -19775 and $asc <= -19219) return "C";
    if($asc >= -19218 and $asc <= -18711) return "D";
    if($asc >= -18710 and $asc <= -18527) return "E";
    if($asc >= -18526 and $asc <= -18240) return "F";
    if($asc >= -18239 and $asc <= -17923) return "G";
    if($asc >= -17922 and $asc <= -17418) return "H";
    if($asc >= -17922 and $asc <= -17418) return "I";
    if($asc >= -17417 and $asc <= -16475) return "J";
    if($asc >= -16474 and $asc <= -16213) return "K";
    if($asc >= -16212 and $asc <= -15641) return "L";
    if($asc >= -15640 and $asc <= -15166) return "M";
    if($asc >= -15165 and $asc <= -14923) return "N";
    if($asc >= -14922 and $asc <= -14915) return "O";
    if($asc >= -14914 and $asc <= -14631) return "P";
    if($asc >= -14630 and $asc <= -14150) return "Q";
    if($asc >= -14149 and $asc <= -14091) return "R";
    if($asc >= -14090 and $asc <= -13319) return "S";
    if($asc >= -13318 and $asc <= -12839) return "T";
    if($asc >= -12838 and $asc <= -12557) return "W";
    if($asc >= -12556 and $asc <= -11848) return "X";
    if($asc >= -11847 and $asc <= -11056) return "Y";
    if($asc >= -11055 and $asc <= -10247) return "Z";
    return NULL;
    //return $s0;
}
function pinyin_long($zh){  //获取整条字符串汉字拼音首字母
    $ret = "";
    $s1 = iconv("UTF-8","gb2312", $zh);
    $s2 = iconv("gb2312","UTF-8", $s1);
    if($s2 == $zh){$zh = $s1;}
    for($i = 0; $i < strlen($zh); $i++){
        $s1 = substr($zh,$i,1);
        $p = ord($s1);
        if($p > 160){
            $s2 = substr($zh,$i++,2);
            $ret .= getfirstchar($s2);
        }else{
            $ret .= $s1;
        }
    }
    return $ret;
}
//删除字符串中的下划线，取第一部分
function delLower($str){
    $tmp=explode('_',$str);
    return $tmp[0];
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function curl_get_contents($url){
    $ch=curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);                //设置访问的url地址
    // curl_setopt($ch,CURLOPT_HEADER,1);               //是否显示头部信息
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);               //设置超时
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);   //用户访问代理 User-Agent

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    curl_setopt($ch, CURLOPT_REFERER,$_SERVER['HTTP_HOST']);        //设置 referer
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);          //跟踪301
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        //返回结果
    $r=curl_exec($ch);

    if(!$r){
        var_dump(curl_error($ch));
    }
    curl_close($ch);
    return $r;
}
?>